package com.web.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.web.config.JWTConfig;
import com.web.model.ResponseData;
import com.web.params.PersonEditParams;
import com.web.params.ProblemParams;
import com.web.service.InfoService;
import com.web.service.UserService;
@RestController
@RequestMapping("/space")
public class SpaceController {
	@Autowired
	private UserService userService;
	@Autowired
	private InfoService infoService;
	//个人信息修改
	@PostMapping("/person/edit")
	public ResponseData PersonEdit(@RequestBody PersonEditParams params) {
		ResponseData responseData = userService.Personedit(params);
		return responseData;
	}
	//个人信息
	@GetMapping("/person")
	public Object Person(@RequestHeader("Authorization")String token) {
		Integer userId = JWTConfig.getId(token);
		ResponseData responseData =infoService.Preson(userId);
		return responseData;

}
	//提问题
	@PostMapping("/pro/add")
	public  ResponseData noteAdd(@RequestHeader("Authorization")String token,@RequestBody @Valid ProblemParams params,BindingResult result) {
        ResponseData responseData = new ResponseData();
        if(result.hasErrors()) {
        	String msg = result.getFieldError().getDefaultMessage();
        	responseData.failed(msg);
        }else {
		Integer userId = JWTConfig.getId(token);
		params.setUserid(userId);
	    responseData= infoService.Pro(params);
        }
		return responseData;
	}
	@GetMapping("/del")
	public ResponseData Del(@RequestHeader("Authorization")String token,@RequestParam("topicId") Integer topicId) {	
		ResponseData responseData = infoService.deletePro(topicId);
		return responseData;
}
}
